//
//  YDBTabBarController.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2018/8/2.
//  Copyright © 2018年 杨东宝. All rights reserved.
//

import UIKit
import Foundation

class YDBTabBarController: UITabBarController,YDBTabBarViewDelegate {
    
    var isTabBarHide: Bool = false
    
    lazy var myTabBar: YDBTabBar! = {
        let myTab = YDBTabBar(frame: self.tabBar.bounds)     // 注意frame与bounds的区别
        myTab.tabBarView.delegate = self
        myTab.tabBarView.tabBarItems = self.tabBarCItems
        return myTab
    }()
    private lazy var tabBarCItems: NSMutableArray = {
        return NSMutableArray()
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 设置子控制器
        setUpAllChildViewController()
        // 自定义tabBar
        addCustTabBar()
    }
    
    // 修改tabbar高度
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        
        var tabFrame = self.tabBar.frame
        tabFrame.size.height = kTabBarHeight
        tabFrame.origin.y = self.view.frame.size.height - kTabBarHeight
        self.tabBar.frame = tabFrame
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        removeTabBarButton()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        removeTabBarButton()
    }
    
    func removeTabBarButton() {
        // 隐藏系统的tabBar子视图（无需删除）
        for view in self.tabBar.subviews {
            if view.isKind(of: NSClassFromString("UITabBarButton")!) {
                view.isHidden = true
            }
        }
    }
    func setUpAllChildViewController() {
        
        let array:[YDBBaseViewController] = [YDBDeiviceViewController(),YDBMineViewController()]
        
        let  selImage = ["ic_device_filled","ic_profil_filled"]
        let noImage =
        ["ic_device_outline","ic_profil_outline"]
        let titles = ["devices".localized,"mine".localized]
        
        for index in 0..<array.count {
            setUpOneOfChildViewController(array[index], noImage[index], selImage[index], titles[index])
        }
    }
    
    func setUpOneOfChildViewController(_ vc: UIViewController, _ image: String, _ selectedImage: String, _ title: String) {
        vc.tabBarItem.title = title
        
        // 设置 tabbarItem 选中状态的图片(不被系统默认渲染,显示图像原始颜色)
        vc.tabBarItem.image = UIImage(named: image)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        vc.tabBarItem.selectedImage = UIImage(named: selectedImage)?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        // 保存tabBarItem模型到数组
        self.tabBarCItems.add(vc.tabBarItem)
        
        let nav: YDBNavigationController = YDBNavigationController(rootViewController: vc)
        self.addChild(nav)
    }
    
    // 自定义tabBar
    func addCustTabBar() {
        // 去除tabBar上的黑线
        let originalTabBar = UITabBar.appearance()
        originalTabBar.shadowImage = UIImage()
        originalTabBar.backgroundImage = UIImage()
        
        // 添加自定义tabBar
        self.tabBar.addSubview(myTabBar)
        self.tabBar.isTranslucent = false
    }
    
    // 显示tabbar
    func showTabBar() {
        if isTabBarHide == true {
            var frame: CGRect = self.tabBar.frame
            frame.origin.y -= kTabBarHeight
            UIView.animate(withDuration: 0.5) {
                self.tabBar.frame = frame
            }
            
            isTabBarHide = false
        }
    }
    
    // 隐藏tabbar
    func hideTabBar() {
        if isTabBarHide == false {
            var frame  = self.tabBar.frame
            frame.origin.y += kTabBarHeight
            UIView.animate(withDuration: 0.5) {
                self.tabBar.frame = frame
            }
            
            isTabBarHide = true
        }
    }
    func setTabbarWithIndex(index:Int) {
        self.myTabBar.tabBarView.setSelectIndex(index: index)
    }
    // MARK: - YDBTabBarViewDelegate
    func tabBar(_ tabBar: YDBTabBarView, didClickedButtonAtIndex index: NSInteger) {
        // 改变UITabBarController中当前显示的viewController
        if self.selectedIndex == index {
           // NotificationCenter.default.post(name: nRootVCRefresh, object: ["index": index])
        }
        self.selectedIndex = index
    }
    
    //    deinit {
    //        NotificationCenter.default.removeObserver(self)
    //    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
